﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public static class SceneTool
{
    /// <summary>
    /// 根据世界坐标获取X轴Z轴的区 
    /// </summary>
    /// <returns>The cell.</returns>
    /// <param name="worldPos">World position.</param>
    /// <param name="bounds">Bounds.</param>
    /// <param name="row">Row.</param>
    /// <param name="column">Column.</param>
    public static Vector2 GetGrid(Vector3 worldPos, Vector4 bounds, int row, int column)
    {
        Vector2 result = new Vector2();
        //场景宽度  x轴
        float SceneWight = bounds.y - bounds.x;
        //场景高度 z轴
        float SceneHeight = bounds.w - bounds.z;
        //行大小
        float rowSize = SceneHeight / row;
        //列大小
        float columnSize = SceneWight / column;

        //xz映射到0开始所有的X都要加上 (0-bounds.x)

        float zeroX = 0 - bounds.x;

        float zeroZ = 0 - bounds.z;

        result.x = Mathf.Ceil((worldPos.x + zeroX) / columnSize);

        result.y = Mathf.Ceil((worldPos.z + zeroZ) / rowSize);

        return result;
    }

    public static List<string> GetNeedLoadScene(Vector3 pos, SceneFormatVo formatVo)
    {
        List<string> result = new List<string>();
        //拿到该位置对应的格子
        Vector2 grid = GetGrid(pos, formatVo.bounds, formatVo.row, formatVo.column);

        float zeroX = 0 - formatVo.bounds.x;
        float zeroZ = 0 - formatVo.bounds.z;

        //将包围盒转到原点为0
        Vector4 TempBounds = formatVo.bounds;
        TempBounds.x += zeroX;
        TempBounds.y += zeroX;
        TempBounds.z += zeroZ;
        TempBounds.w += zeroZ;

        //将目标点也相应加上偏移值
        Vector3 TempPos = pos;
        TempPos.x += zeroX;
        TempPos.z += zeroZ;


        if (grid.x >= 0 && grid.x <= formatVo.row && grid.y >= 0 && grid.y <= formatVo.column)
            result.Add(formatVo.sceneName + grid.x + "_" + grid.y);

        for (int i = 0; i < formatVo.range; i++)
        {
            int index = i + 1;
            //左上
            Vector2 leftTop = grid;
            leftTop.x -= index;
            leftTop.y += index;
            if (leftTop.x >= 0 && leftTop.x <= formatVo.row && leftTop.y >= 0 && leftTop.y <= formatVo.column)
                result.Add(formatVo.sceneName + leftTop.x + "_" + leftTop.y);

            //左
            Vector2 left = grid;
            left.x -= index;
            if (left.x >= 0 && left.x <= formatVo.row && left.y >= 0 && left.y <= formatVo.column)
                result.Add(formatVo.sceneName + left.x + "_" + left.y);


            //左下
            Vector2 leftDown = grid;
            leftDown.x -= index;
            leftTop.y -= index;
            if (leftDown.x >= 0 && leftDown.x <= formatVo.row && leftDown.y >= 0 && leftDown.y <= formatVo.column)
                result.Add(formatVo.sceneName + leftDown.x + "_" + leftDown.y);

            //上
            Vector2 top = grid;
            top.y += index;
            if (top.x >= 0 && top.x <= formatVo.row && top.y >= 0 && top.y <= formatVo.column)
                result.Add(formatVo.sceneName + top.x + "_" + top.y);

            //下
            Vector2 down = grid;
            down.y -= index;
            if (down.x >= 0 && down.x <= formatVo.row && down.y >= 0 && down.y <= formatVo.column)
                result.Add(formatVo.sceneName + down.x + "_" + down.y);

            //右上
            Vector2 rightTop = grid;
            rightTop.x += index;
            rightTop.y += index;
            if (rightTop.x >= 0 && rightTop.x <= formatVo.row && rightTop.y >= 0 && rightTop.y <= formatVo.column)
                result.Add(formatVo.sceneName + rightTop.x + "_" + rightTop.y);

            //右
            Vector2 right = grid;
            right.x += index;
            if (right.x >= 0 && right.x <= formatVo.row && right.y >= 0 && right.y <= formatVo.column)
                result.Add(formatVo.sceneName + right.x + "_" + right.y);

            //右下
            Vector2 rightDown = grid;
            rightDown.x += index;
            rightDown.y -= index;
            if (rightDown.x >= 0 && rightDown.x <= formatVo.row && rightDown.y >= 0 && rightDown.y <= formatVo.column)
                result.Add(formatVo.sceneName + rightDown.x + "_" + rightDown.y);

        }
        return result;
    }

    //将二D包围盒转换到最小XZ为0

}
